<?php
namespace Admin\Controller;
class AdboardController extends CommonController {
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('adboard');
    }

    public function _before_index() {
        $tpl_list = $this->_mod->get_tpl_list();
        $this->assign('tpl_list', $tpl_list);
        $big_menu = array(
            'title' => L('adboard_add'),
            'iframe' => U('Adboard/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '280'
        );
        $this->_ajaxedit = 1;
        $this->_data = array(
            'addad'    => array(
                'c' => 'Ad',
                'a' => 'add',
                'str'    => '<a data-height="410" data-width="520" data-id="add" data-uri="%s" data-title="添加 - 添加广告" class="J_showdialog" href="javascript:;">添加广告</a>',
                'param' => array('board_id'=>'@id@'),
                'isajax' => 1,
                'replace'=> ''
            ),
            'addlist'    => array(
                'c' => 'Ad',
                'a' => 'index',
                'str'    => '<a href="%s">广告管理</a>',
                'param' => array('board_id'=>'@id@'),
                'isajax' => 0,
                'replace'=> ''
            ),
        );
        $this->assign('big_menu', $big_menu);
    }

    public function _before_add() {
        $tpl_list = $this->_mod->get_tpl_list();
        $this->assign('tpl_list', $tpl_list);
    }

    public function add(){
        if(IS_POST){
            $this->_mod->create();
            if($this->_mod->add()){
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'add');
                $this->success(L('operation_success'));
            }
        }else{
            $response = $this->fetch();
            $this->ajaxReturn(1, '', $response);
            $this->display();
        }

    }
    public function delete(){
        $pk = $this->_mod->getPk();
        $ids = trim(I($pk), ',');
        if ($ids) {
            if (false !== $this->_mod->delete($ids)) {
                M('ad')->where(array('board_id'=>array('in',$ids)))->delete();
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
            $this->error(L('illegal_parameters'));
        }
    }
    public function _before_edit() {
        $tpl_list = $this->_mod->get_tpl_list();
        $this->assign('tpl_list', $tpl_list);
    }


    public function ajax_check_name() {
        $name = Y('name', 'trim');
        $id = Y('id', 'intval');
        if ($this->_mod->name_exists($name, $id)) {
            $this->ajaxReturn(0, L('adboard_already_exists'));
        } else {
            $this->ajaxReturn();
        }
    }
}